/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sport;

import javax.microedition.media.MediaException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.Manager;
import javax.microedition.media.control.ToneControl;

/**
 * @author 7904112B
 */
public class BeepTest extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
private Form form;
private TextField tfFreq;
private TextField tfDuration;
private TextField tfTimes;
private Command okCommand;
private Command exitCommand;
//</editor-fold>//GEN-END:|fields|0|

    /**
     * The BeepTest constructor.
     */
    public BeepTest() {
    }

//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
//</editor-fold>//GEN-END:|methods|0|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
/**
 * Initilizes the application.
 * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
 */
private void initialize () {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
}//GEN-BEGIN:|0-initialize|2|
//</editor-fold>//GEN-END:|0-initialize|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
/**
 * Performs an action assigned to the Mobile Device - MIDlet Started point.
 */
public void startMIDlet () {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
switchDisplayable (null, getForm ());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
}//GEN-BEGIN:|3-startMIDlet|2|
//</editor-fold>//GEN-END:|3-startMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
/**
 * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
 */
public void resumeMIDlet () {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
}//GEN-BEGIN:|4-resumeMIDlet|2|
//</editor-fold>//GEN-END:|4-resumeMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
/**
 * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
 * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
 * @param nextDisplayable the Displayable to be set
 */
public void switchDisplayable (Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
Display display = getDisplay ();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
if (alert == null) {
display.setCurrent (nextDisplayable);
} else {
display.setCurrent (alert, nextDisplayable);
}//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
}//GEN-BEGIN:|5-switchDisplayable|2|
//</editor-fold>//GEN-END:|5-switchDisplayable|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
/**
 * Called by a system to indicated that a command has been invoked on a particular displayable.
 * @param command the Command that was invoked
 * @param displayable the Displayable where the command was invoked
 */
public void commandAction (Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
 // write pre-action user code here
if (displayable == form) {//GEN-BEGIN:|7-commandAction|1|18-preAction
if (command == exitCommand) {//GEN-END:|7-commandAction|1|18-preAction
 // write pre-action user code here
//GEN-LINE:|7-commandAction|2|18-postAction
 // write post-action user code here
} else if (command == okCommand) {//GEN-LINE:|7-commandAction|3|16-preAction
//GEN-BEGIN:|7-commandAction|4|16-postAction

//new com.nokia.mid.sound.Sound(Integer.parseInt(tfFreq.getString()), Long.parseLong(tfDuration.getString())).play(Integer.parseInt(tfTimes.getString()));
//AlertType.INFO.playSound(getDisplay());

// try {
//    Manager.playTone(ToneControl.C4, 5000 /* millisec */, 100 /* max vol */);
//} catch (MediaException e) { e.printStackTrace();}

try {
    // write pre-action user code here
    //javax.microedition.media.Manager.playTone(Integer.parseInt(tfFreq.getString()), Integer.parseInt(tfDuration.getString()), Integer.parseInt(tfTimes.getString()));
    javax.microedition.media.Manager.playTone(Integer.parseInt(tfFreq.getString()), Integer.parseInt(tfDuration.getString()), Integer.parseInt(tfTimes.getString()));
} catch (MediaException ex) {
    ex.printStackTrace();
}//GEN-END:|7-commandAction|4|16-postAction


 // write post-action user code here
}//GEN-BEGIN:|7-commandAction|5|7-postCommandAction
}//GEN-END:|7-commandAction|5|7-postCommandAction
 // write post-action user code here
}//GEN-BEGIN:|7-commandAction|6|
//</editor-fold>//GEN-END:|7-commandAction|6|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">//GEN-BEGIN:|13-getter|0|13-preInit
/**
 * Returns an initiliazed instance of form component.
 * @return the initialized component instance
 */
public Form getForm () {
if (form == null) {//GEN-END:|13-getter|0|13-preInit
 // write pre-init user code here
form = new Form ("form", new Item[] { getTfFreq (), getTfDuration (), getTfTimes () });//GEN-BEGIN:|13-getter|1|13-postInit
form.addCommand (getOkCommand ());
form.addCommand (getExitCommand ());
form.setCommandListener (this);//GEN-END:|13-getter|1|13-postInit
 // write post-init user code here
}//GEN-BEGIN:|13-getter|2|
return form;
}
//</editor-fold>//GEN-END:|13-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|15-getter|0|15-preInit
/**
 * Returns an initiliazed instance of okCommand component.
 * @return the initialized component instance
 */
public Command getOkCommand () {
if (okCommand == null) {//GEN-END:|15-getter|0|15-preInit
 // write pre-init user code here
okCommand = new Command ("Ok", Command.OK, 0);//GEN-LINE:|15-getter|1|15-postInit
 // write post-init user code here
}//GEN-BEGIN:|15-getter|2|
return okCommand;
}
//</editor-fold>//GEN-END:|15-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|17-getter|0|17-preInit
/**
 * Returns an initiliazed instance of exitCommand component.
 * @return the initialized component instance
 */
public Command getExitCommand () {
if (exitCommand == null) {//GEN-END:|17-getter|0|17-preInit
 // write pre-init user code here
exitCommand = new Command ("Exit", Command.EXIT, 0);//GEN-LINE:|17-getter|1|17-postInit
 // write post-init user code here
}//GEN-BEGIN:|17-getter|2|
return exitCommand;
}
//</editor-fold>//GEN-END:|17-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: tfFreq ">//GEN-BEGIN:|19-getter|0|19-preInit
/**
 * Returns an initiliazed instance of tfFreq component.
 * @return the initialized component instance
 */
public TextField getTfFreq () {
if (tfFreq == null) {//GEN-END:|19-getter|0|19-preInit
 // write pre-init user code here
tfFreq = new TextField ("Note", "127", 32, TextField.NUMERIC);//GEN-LINE:|19-getter|1|19-postInit
 // write post-init user code here
}//GEN-BEGIN:|19-getter|2|
return tfFreq;
}
//</editor-fold>//GEN-END:|19-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: tfDuration ">//GEN-BEGIN:|20-getter|0|20-preInit
/**
 * Returns an initiliazed instance of tfDuration component.
 * @return the initialized component instance
 */
public TextField getTfDuration () {
if (tfDuration == null) {//GEN-END:|20-getter|0|20-preInit
 // write pre-init user code here
tfDuration = new TextField ("Duration", "1000", 32, TextField.NUMERIC);//GEN-LINE:|20-getter|1|20-postInit
 // write post-init user code here
}//GEN-BEGIN:|20-getter|2|
return tfDuration;
}
//</editor-fold>//GEN-END:|20-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: tfTimes ">//GEN-BEGIN:|25-getter|0|25-preInit
/**
 * Returns an initiliazed instance of tfTimes component.
 * @return the initialized component instance
 */
public TextField getTfTimes () {
if (tfTimes == null) {//GEN-END:|25-getter|0|25-preInit
 // write pre-init user code here
tfTimes = new TextField ("Volume", "100", 32, TextField.NUMERIC);//GEN-LINE:|25-getter|1|25-postInit
 // write post-init user code here
}//GEN-BEGIN:|25-getter|2|
return tfTimes;
}
//</editor-fold>//GEN-END:|25-getter|2|

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

}
